/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.servlet;

import com.util.SQLConnection;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

/**
 *
 * @author chetans
 */

	public class SaveData extends HttpServlet {

    private Connection con;
    private PreparedStatement pstmt;
    private Logger myLogger = Logger.getLogger("RootLogger");

     private void saveTimeData(HttpServletRequest request,PrintWriter out) throws Exception{
            myLogger.info("Enetering..");

	        String lastRow = (String)request.getParameter("numOfRows");
            int numberOfRows = Integer.parseInt(lastRow);
            myLogger.info("number of rows :"+numberOfRows);

            String month =(String) request.getParameter("month");
            String year = (String) request.getParameter("year");
            int imon = Integer.parseInt(month);
            int iyear = Integer.parseInt(year);
            myLogger.info("year :"+iyear+" month :"+imon);

            String week = (String)request.getParameter("week");
            myLogger.info("week :"+week);

            String[] weekArr = week.split("-");
            int startDate = Integer.parseInt(weekArr[0]);
            int endDate = Integer.parseInt(weekArr[1]);
            int numOfDays = (endDate-startDate)+1;
            myLogger.info("numOfDays:"+numOfDays);
            
            String taskId;
            //TODO:Use a Vector instead
	        String days[] = new String[numOfDays];
            String output= "";
	        while(numberOfRows > 0){
	            taskId = (String) request.getParameter("taskBox"+numberOfRows);
                if(taskId!= null){
                for(int k=0;k<numOfDays;k++)
                    days[k]=request.getParameter("time"+(numberOfRows+1)+(k+1));

                con = new SQLConnection().getMySQLConnection();
                pstmt = con.prepareStatement("replace into task_records set task_name = ?,day1=?,day2=?,day3=?,day4=?,day5=?,day6=?,day7=?,month=?,year=?,week=?");
                //Initia;ize all the SQL parameters
                for(int i=1;i<=11;i++){
                    pstmt.setObject(i, null);
                }
                pstmt.setString(1,taskId);
                for(int k=0;k<numOfDays;k++){
                    //TODO: USE Enuumeration here
                    pstmt.setInt(k+2, Integer.parseInt(days[k]));
                }
                pstmt.setInt(9, imon);
                pstmt.setInt(10, iyear);
                pstmt.setString(11, week);
                int result = pstmt.executeUpdate();
                if(result !=0)
                    output="Time Data saved successfully ";
                else
                    output = "Saving data failed";

                 
                }   numberOfRows --;
	        }
	        out.println(output);
            myLogger.info("Exiting..");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException,NumberFormatException {
        myLogger.info("Enetering..");
        PrintWriter out = response.getWriter();
        try{
           saveTimeData(request,out);
       }catch(Exception e){
           e.printStackTrace();
           System.out.println("Exception in saving data :"+e);
       }
        myLogger.info("Exiting..");
    }
}
